//
//  RWBindPhoneViewController.m
//  RedWine
//
//  Created by Liufangfang on 2017/1/2.
//  Copyright © 2017年 HuiBangKe. All rights reserved.
//

#import "RWBindPhoneViewController.h"
#import "FTYInfoVertifyTools.h"
#import "RWBindNewPhoneViewController.h"

@interface RWBindPhoneViewController ()<UITextFieldDelegate>{

    NSArray *cellImages;
    NSArray *cellTexts;
}

@property (nonatomic, strong) UITextField *phoneNumberTF;
@property (nonatomic, strong) UITextField *codeTF;

@end

@implementation RWBindPhoneViewController

#pragma mark - View LifeCycle

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initObjects];
    [self setNavigation];
    [self setBindPhoneUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Load Datas

- (void)initObjects{
    
    cellImages = @[@"myiphone_icon", @"code_icon"];
    cellTexts = @[@"请输入原手机号", @"请输入验证码"];
}

- (void)loadDatas{
    
    
}

#pragma mark - Config UI

- (void)setNavigation{
    
    self.title = @"验证手机号";
}

- (void)setBindPhoneUI{
    
    _bindPhoneTV = [[FTYTouchTableView alloc] initWithFrame:CGRectMake(0, 0, FTY_SCREEN_WIDTH, FTY_SCREEN_HEIGHT) style:UITableViewStylePlain];
    [self.view addSubview:_bindPhoneTV];
    _bindPhoneTV.backgroundColor = [FTYColor getEEEEEEColorWithAlpha:1.];
    _bindPhoneTV.separatorStyle = UITableViewCellSeparatorStyleNone;
    _bindPhoneTV.scrollEnabled = NO;
    _bindPhoneTV.dataSource = self;
    _bindPhoneTV.delegate = self;
    _bindPhoneTV.touchDelegate = self;
    _bindPhoneTV.tableFooterView = [self setFooterUI];
}

#pragma mark - Config Invoke Methods

- (UIView *)setFooterUI{

    UIView *footerV = [[UIView alloc] initWithFrame:self.view.bounds];
    
    FTYButton *nextBtn = [FTYButton createButtonWithTitle:@"下一步" tColor:[UIColor whiteColor] fontSize:15.*FTY_WIDTH_BASE backGroundColor:[FTYColor getMainColorWithAlpha:1.] cornerRadius:20];
    [footerV addSubview:nextBtn];
    [nextBtn addTarget:self action:@selector(nextBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(30.*FTY_HEIGHT_BASE);
        make.left.mas_equalTo(10.*FTY_WIDTH_BASE);
        make.width.mas_equalTo(FTY_SCREEN_WIDTH - 2 * 10.*FTY_WIDTH_BASE);
        make.height.mas_equalTo(40.*FTY_HEIGHT_BASE);
    }];
    
    return footerV;
}

#pragma mark - Notification Method

#pragma mark - Notification Invoke Methods

#pragma mark - <UITableViewDataSource>

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 30.*FTY_HEIGHT_BASE;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 60.*FTY_HEIGHT_BASE;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString * const bindPhoneID = @"bindPhoneID";
    UITableViewCell *bindPhoneCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:bindPhoneID];
    
    NSInteger row = indexPath.row;
    bindPhoneCell.imageView.image = [UIImage imageNamed:cellImages[row]];
    
    UITextField *textField = [[UITextField alloc] init];
    [bindPhoneCell addSubview:textField];
    textField.placeholder = cellTexts[row];
    textField.font = FTY_FONTTSIZE(15);
    textField.delegate = self;
    textField.keyboardType = UIKeyboardTypeNumberPad;
    [textField sizeToFit];
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(bindPhoneCell);
        make.left.mas_equalTo(bindPhoneCell.imageView.mas_right).offset(10.*FTY_WIDTH_BASE);
        make.right.mas_offset(-10.*FTY_WIDTH_BASE);
    }];
    
    if (0 == row) {
        _phoneNumberTF = textField;
    } else if (1 == row) {
        
        _codeTF = textField;
        FTYButton *getCodeBtn = [FTYButton createButtonWithTitle:@"获取验证码" tColor:[UIColor blackColor] fontSize:13.*FTY_WIDTH_BASE backGroundColor:[FTYColor getMainColorWithAlpha:1.] cornerRadius:15.*FTY_WIDTH_BASE];
        [bindPhoneCell addSubview:getCodeBtn];
        [getCodeBtn addTarget:self action:@selector(getCodeBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [getCodeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(bindPhoneCell.mas_centerY);
            make.right.mas_equalTo(-10.*FTY_WIDTH_BASE);
            make.width.mas_equalTo(80.*FTY_WIDTH_BASE);
            make.height.mas_equalTo(30.*FTY_HEIGHT_BASE);
        }];
    }
    
    UIView *line = [[UIView alloc] init];
    [bindPhoneCell addSubview:line];
    line.backgroundColor = [FTYColor grayColor];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(0);
        make.height.mas_equalTo(.5*FTY_HEIGHT_BASE);
    }];
    
    return bindPhoneCell;
}

#pragma mark - <UITableViewDelegate>

#pragma mark - <FTYTouchTableViewDelegate>

- (void)tableView:(UITableView *)tableView touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    [_phoneNumberTF resignFirstResponder];
    [_codeTF resignFirstResponder];
}

#pragma mark - Private Invoke Methods

- (void)getCodeBtnAction:(UIButton *)btn{
    
    NSString *phoneNumber = _phoneNumberTF.text;
    [FTYInfoVertifyTools phoneNumberVertify:phoneNumber showAtView:self.view];
    
    [FTYHTTPManager postInfoWithSubUrl:[NSString stringWithFormat:@"/shop/ybkapi.php?act=ybkapi&op=verify&type=updatemobile&member_mobile=%@", phoneNumber] parameters:nil block:^(id result, NSError *error) {
        if (!error) {
            
            NSInteger code = [[[result objectForKey:@"err"] objectForKey:@"errorcode"] integerValue];
            NSString *info = [[result objectForKey:@"err"] objectForKey:@"errorinfo"];
            if (0 == code) {
                [[FTYToastView alloc] addTitle:@"验证码已发送～" addView:self.view];
            } else {
                [[FTYToastView alloc] addTitle:info addView:self.view];
            }
        } else {
            NSLog(@"=====%@", error);
        }
    }];
}

- (void)nextBtnAction:(UIButton *)btn{
    
    NSString *codeVer = _codeTF.text;
    NSString *phoneNumber = _phoneNumberTF.text;
    
    [FTYInfoVertifyTools phoneNumberVertify:phoneNumber showAtView:self.view];
    
    [FTYHTTPManager postInfoWithSubUrl:[NSString stringWithFormat:@"/shop/ybkapi.php?act=ybkapi&op=updatemobile&member_id=%@&token=%@&member_mobile=%@&verify=%@", [FTYUser sharedUser].userID, [FTYUser sharedUser].userToken, phoneNumber, codeVer] parameters:nil block:^(id result, NSError *error) {
        if (!error) {
            
            NSInteger code = [[[result objectForKey:@"err"] objectForKey:@"errorcode"] integerValue];
            NSString *info = [[result objectForKey:@"err"] objectForKey:@"errorinfo"];
            if (0 == code) {
                
                RWBindNewPhoneViewController *bindNewPhoneVC = [[RWBindNewPhoneViewController alloc] init];
                [self.navigationController pushViewController:bindNewPhoneVC animated:YES];
            } else {
                [[FTYToastView alloc] addTitle:info addView:self.view];
            }
        } else {
            NSLog(@"=====%@", error);
        }
    }];
}

@end
